package com.lzy.huijinjiazu.mvp.ui.activity;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.flyco.tablayout.utils.FragmentChangeManager;
import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.app.EventBusTags;
import com.lzy.huijinjiazu.di.component.DaggerMyCollectComponent;
import com.lzy.huijinjiazu.di.module.MyCollectModule;
import com.lzy.huijinjiazu.mvp.contract.MyCollectContract;
import com.lzy.huijinjiazu.mvp.presenter.MyCollectPresenter;
import com.lzy.huijinjiazu.mvp.ui.fragment.FinanceCollectFragment;
import com.lzy.huijinjiazu.mvp.ui.fragment.MallCollectFragment;
import com.lzy.huijinjiazu.mvp.ui.fragment.PersonalTailorFragment;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MyCollectActivity extends BaseActivity<MyCollectPresenter> implements MyCollectContract.View {


    @BindView(R.id.tl_mall)
    SegmentTabLayout mTlMall;
    @BindView(R.id.fl_title)
    FrameLayout mFlTitle;
    @BindView(R.id.iv_del)
    ImageView mIvDel;
    @BindView(R.id.tv_complete)
    TextView mTvComplete;
    private boolean isGoodsEdit;
    //    private boolean isFinanceEdit;
    private FragmentChangeManager mFragmentChangeManager;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerMyCollectComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .myCollectModule(new MyCollectModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_my_collect; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBarMarginTop(this, mFlTitle);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initFragment();
        mTlMall.setTabData(ArmsUtils.getStringArray(this, R.array.mall_collect_tab));
        mTlMall.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mFragmentChangeManager.setFragments(position);
                isGoodsEdit = false;
                mIvDel.setVisibility(isGoodsEdit ? View.GONE : View.VISIBLE);
                mTvComplete.setVisibility(isGoodsEdit ? View.VISIBLE : View.GONE);
                collectEvent(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    private void collectEvent(int type) {
        Message message = new Message();
        message.obj = isGoodsEdit;
        message.what = type;
        EventBus.getDefault().post(message, EventBusTags.COLLECT_STATE);
    }

    private void clickEdit() {
        if (mTlMall.getCurrentTab() == 0) {
            clickType();
            collectEvent(0);
        } else {
            clickType();
            collectEvent(1);
        }
    }

    private void clickType() {
        isGoodsEdit = !isGoodsEdit;
        mIvDel.setVisibility(isGoodsEdit ? View.GONE : View.VISIBLE);
        mTvComplete.setVisibility(isGoodsEdit ? View.VISIBLE : View.GONE);
    }

    private void initFragment() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(MallCollectFragment.newInstance());
        fragments.add(FinanceCollectFragment.newInstance());
        mFragmentChangeManager = new FragmentChangeManager(getSupportFragmentManager(), R.id.fl_content, fragments);
        mFragmentChangeManager.setFragments(0);
    }


    @OnClick({R.id.iv_del, R.id.tv_complete, R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_del:
            case R.id.tv_complete:
                clickEdit();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
